<template>
  <div class="container">
    <div class="input_box">
      <div class="input_list flex-row flex-y-center vux-1px-b">
        <label>手机号</label>
        <input v-model="phone" type="number" pattern="[0-9]*" onkeyup="value=value.replace(/[^\d\.]/g,'')"
               onblur="value=value.replace(/[^\d\.]/g,'')" placeholder="请输入手机号">
      </div>
      <div class="input_list input_list_code flex-row flex-y-center vux-1px-b">
        <div class="code flex-grow-1 flex-y-center">
          <label>验证码</label>
          <input v-model="code" type="number" pattern="[0-9]*" onkeyup="value=value.replace(/[^\d\.]/g,'')"
                 onblur="value=value.replace(/[^\d\.]/g,'')" placeholder="请输入验证码">
        </div>
        <button @click="getCode" :disabled="disabled" :class="{'active':showActive}"
                class="code_btn">
          {{code_text}}
        </button>
      </div>
      <div class="input_list flex-row flex-y-center vux-1px-b">
        <label>设置密码</label>
        <input v-model="password" type="text" placeholder="6-12位数字或字母">
      </div>
      <div class="input_list flex-row flex-y-center vux-1px-b">
        <label>确认密码</label>
        <input v-model="confirmPassword" placeholder="6-12位数字或字母">
      </div>
      <button @click="registerBtn" class="startBtn">注册</button>
    </div>
  </div>
</template>

<script>
  import {GETCODE,REGISTER} from 'src/const/uri'
  export default {
    name: "Register",
    data() {
      return {
        phone: '',
        code: '',
        password: '',
        confirmPassword: '',
        code_text: '获取验证码',
        disabled: false,
        showActive: false,
        passwordreg: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/,
        phonereg: /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/
      }
    },
    methods: {
      registerBtn() {
        if (!this.phone) {
          this.$vux.toast.text('请输入手机号');
          return false
        } else if (!this.phonereg.test(this.phone)) {
          this.$vux.toast.text('请输入正确的手机号');
          return false
        } else if (!this.code) {
          this.$vux.toast.text('请输入验证码');
          return false
        } else if (!this.password) {
          this.$vux.toast.text('请设置密码');
          return false
        } else if (!this.passwordreg.test(this.password)) {
          this.$vux.toast.text('密码要求大于6位小于12位，字母和数字');
          return false
        } else if (!this.confirmPassword) {
          this.$vux.toast.text('请确认密码');
          return false;
        } else if (this.confirmPassword !== this.password) {
          this.$vux.toast.text('输入密码2次不一致');
          return false;
        }
        this.$ajax.post(REGISTER, {
          phone: this.phone,
          code: this.code,
          password: this.password
        }).then(res => {
          this.$vux.toast.text(res.data.msg);
          if(res.code==1){
            setTimeout(()=>{
              window.location.href = window.location.origin+'/login'
            },1000);
          }
        })
      },
      getCode() {
        var time = 60;
        if (this.phone) {
          if (this.phonereg.test(this.phone)) {
            this.$ajax.get(GETCODE, {
                phone: this.phone
            }).then((res) => {
              if (res.code == 0) {
                this.$vux.toast.text(res.msg);
              } else if (res.code == 1) {
                this.$vux.toast.text('发送成功');
                if (!this.disabled) {
                  setTimeout(() => {
                    var timer = setInterval(() => {
                      time -= 1;
                      if (time == 0) {
                        clearInterval(timer);
                        this.showActive = false;
                        this.code_text = `重新发送`;
                        this.disabled = false;
                      } else {
                        this.showActive = true;
                        this.code_text = `${time}s`;
                        this.disabled = true;
                      }
                    }, 1000)
                  }, 100)
                }
              }
            })
          } else {
            this.$vux.toast.text('请输入正确的手机号');
          }
        } else {
          this.$vux.toast.text('手机号不能为空');
        }
      },
    }
  }
</script>

<style scoped lang="less">
  .container{
    background:#fff;
  }
  .input_box {
    /*padding: 0.4rem 0.2rem 0.32rem;*/

    .input_list {
      background: #fff;
      font-size: 0.3rem;
      height: 0.91rem;
      padding-left: 0.3rem;
      label {
        width: 1.5rem;
      }

      input {
        font-size: 0.3rem;
        border: 0;
        background: #fff;
        height: 99%;
      }

      &.input_list_code {
        background: transparent;
        padding-left: 0;
        font-size: 0.3rem;

        .code {
          background: #fff;
          padding-left: 0.3rem;
          height: 100%;
          input {
            width: 2rem;
          }
        }

        .code_btn {
          width: 2.49rem;
          height:100%;
          text-align: center;
          background: #fff;
          margin-left: 0.17rem;
          border: 0;

          &.active {
            color: red;
          }
        }
      }
    }
  }

  .startBtn {
    display: block;
    width: 7.11rem;
    height: .91rem;
    background: red;
    color: #fff;
    font-size: .3rem;
    font-weight: 600;
    border: none;
    border-radius: 10px;
    margin: 0.3rem auto 0;
  }
</style>
